package org.hotswap.hotswaphelper.ui;

import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;

/**
 * pacakgeName com.ccnode.codegenerator.view.datasource
 *
 * @author bruce ge
 */
public class CopyTextDialog extends DialogWrapper {

    private JPanel thePanel;
    private JTextArea theTextArea;
    private String text;
    @Nullable
    private Project project;

    public CopyTextDialog(@Nullable Project project, String text, String title) {
        super(project, true, IdeModalityType.MODELESS);
        this.project = project;
        $$$setupUI$$$();
        this.text = text;
        theTextArea.setText(text);
        this.setSize(800, 800);
        setOKButtonText("Ok");
        setTitle(title);
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        return new JBScrollPane(thePanel);
    }


    @Override
    protected void doOKAction() {
        CopyPasteManager.getInstance().setContents(new StringSelection(text));
        JBPopupFactory.getInstance().createMessage("copy content success").showCenteredInCurrentWindow(project);
        super.doOKAction();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        thePanel = new JPanel();
        thePanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        final JScrollPane scrollPane1 = new JScrollPane();
        thePanel.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        theTextArea = new JTextArea();
        theTextArea.setRows(20);
        scrollPane1.setViewportView(theTextArea);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return thePanel;
    }

}
